import requests
import json
def dify_createdb(kg_name):
    # API端点和API密钥
    # url = 'http://172.16.32.45:11206/v1/chat-messages' #v1/chat-messages'调用聊天框  workflows/run智能体
    url = 'http://192.168.73.129:80/v1/datasets'  # v1/chat-messages'调用聊天框  workflows/run智能体
    api_key = 'dataset-Nx874tLa6L3Yc92jWRnpEw1G'  # 替换为您的实际API密钥

    # 请求头
    headers = {
        'Authorization': f'Bearer {api_key}',
        'Content-Type': 'application/json'
    }

        # 请求数据
    # --data - raw
    # '{"name": "name", "permission": "only_me"}'

    data = {
        "name": kg_name,
        "text": "text",
        "permission": "only_me",  # blocking streaming
    }

    # 将数据转换为JSON字符串
    data_json = json.dumps(data)

    # 发送POST请求
    response = requests.post(url, headers=headers, data=data_json.encode("utf-8"))
    response_data=json.loads(response.text)


    return response_data





response_data=dify_createdb(kg_name='正确sql知识库') #知识库已经存在 通过文本创建文档
print(response_data)
if response_data.get("status")==409:
    print('同名知识库已经存在,请重命名')
else:
    id = response_data.get('id')
    print(id)
#需要从获取的空知识库中获取知识库ID

